package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.CalendarMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.Calendar;
import edu.hubu.talentmanagementsystem.service.ICalendarService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class CalendarServiceImpl extends ServiceImpl<CalendarMapper, Calendar> implements ICalendarService {

    private final MapperContext context;

    public CalendarServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getCalendarMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<Calendar> insert(@EncodeParam Calendar record) {
        int insert = context.getCalendarMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getCalendarMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<Calendar> selectById(Integer id) {
        QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Calendar> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Calendar::getCalendarId, id);
        return Optional.of(context.getCalendarMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<Calendar> selectAll(String order) {
        QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Calendar> select = getColumnWrapper(wrapper.lambda());
            return context.getCalendarMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Calendar> select = getColumnWrapper(wrapper.lambda());
        return context.getCalendarMapper().selectList(select);
    }

    @Override
    public Page<Calendar> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
        Page<Calendar> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Calendar> select = getColumnWrapper(wrapper.lambda());
            return context.getCalendarMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Calendar> select = getColumnWrapper(wrapper.lambda());
        return context.getCalendarMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<Calendar> getColumnWrapper(LambdaQueryWrapper<Calendar> wrapper) {
        return wrapper.select(
                Calendar::getStaff
                    ,
                Calendar::getCalendarId
                    ,
                Calendar::getCalendarStartDate
                    ,
                Calendar::getCalendarEndDate
                    ,
                Calendar::getCalendarContent
                    ,
                Calendar::getCalendarLevel
                    ,
                Calendar::getCalendarIsFinished
                    ,
                Calendar::getCalendarStaffId
                    ,
                Calendar::getCalendarTitle
        );
    }

    @Override
    public Page<Calendar> selectLikePaged( Integer currentPage,
             Integer pageSize, Calendar record, String order) {
        Page<Calendar> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getCalendarMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getCalendarMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<Calendar> getQueryWrapper(QueryWrapper<Calendar>wrapper, Calendar record) {
        LambdaQueryWrapper<Calendar> lambda = wrapper.lambda();
        if (record.getStaff() != null) {
            lambda = lambda.eq(Calendar::getStaff,record.getStaff());
        }
        if (record.getCalendarId() != null) {
            lambda = lambda.eq(Calendar::getCalendarId,record.getCalendarId());
        }
        if (record.getCalendarStartDate() != null) {
            lambda = lambda.eq(Calendar::getCalendarStartDate,record.getCalendarStartDate());
        }
        if (record.getCalendarEndDate() != null) {
            lambda = lambda.eq(Calendar::getCalendarEndDate,record.getCalendarEndDate());
        }
        if (record.getCalendarLevel() != null) {
            lambda = lambda.eq(Calendar::getCalendarLevel,record.getCalendarLevel());
        }
        if (record.getCalendarIsFinished() != null) {
            lambda = lambda.eq(Calendar::getCalendarIsFinished,record.getCalendarIsFinished());
        }
        if (record.getCalendarStaffId() != null) {
            lambda = lambda.eq(Calendar::getCalendarStaffId,record.getCalendarStaffId());
        }
        if (record.getCalendarContent() != null) {
            lambda = lambda.like(Calendar::getCalendarContent,record.getCalendarContent ());
        }
        if (record.getCalendarTitle() != null) {
            lambda = lambda.like(Calendar::getCalendarTitle,record.getCalendarTitle ());
        }
        return lambda;
    }

    public int count(Calendar record) {
        return context.getCalendarMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<Calendar> updateById(Integer id, @EncodeParam Calendar record) {
        record.setCalendarId(id);
        int update = context.getCalendarMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<Calendar> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<Calendar> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Calendar::getCalendarId, id);
            return Optional.of(context.getCalendarMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
